<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { login, register } from '../services/auth';
import { useUserStore } from '../stores/user';
import AvatarUpload from '../components/AvatarUpload.vue';

const router = useRouter();
const userStore = useUserStore();
const mobile = ref('');
const password = ref('');
const confirmPassword = ref('');
const nickname = ref('');
const avatarBase64 = ref('');
const errorMessage = ref('');
const isLoading = ref(false);
const isLoginMode = ref(true);

const handleLogin = async () => {
  if (!mobile.value || !password.value) {
    errorMessage.value = '请输入手机号和密码';
    return;
  }
  
  try {
    isLoading.value = true;
    errorMessage.value = '';
    
    // 调用登录服务，传递手机号和密码
    const result = await login(mobile.value, password.value);
    if (result.success) {
      // 更新Pinia中的用户信息
      userStore.updateUserInfo(result.userInfo);
      router.push('/');
    } else {
      errorMessage.value = result.message || '登录失败，请检查账号和密码';
    }
  } catch (error) {
    console.error('登录出错:', error);
    errorMessage.value = '登录失败，请稍后再试';
  } finally {
    isLoading.value = false;
  }
};

const handleRegister = async () => {
  if (!mobile.value || !password.value || !confirmPassword.value || !nickname.value) {
    errorMessage.value = '请填写所有必填项';
    return;
  }
  
  if (password.value !== confirmPassword.value) {
    errorMessage.value = '两次输入的密码不一致';
    return;
  }
  
  try {
    isLoading.value = true;
    errorMessage.value = '';
    
    const result = await register(mobile.value, password.value, nickname.value, avatarBase64.value);
    if (result.success) {
      // 注册成功后自动登录
      userStore.updateUserInfo(result.userInfo);
      router.push('/');
    } else {
      errorMessage.value = result.message || '注册失败，请稍后再试';
    }
  } catch (error) {
    console.error('注册出错:', error);
    errorMessage.value = '注册失败，请稍后再试';
  } finally {
    isLoading.value = false;
  }
};

const toggleMode = () => {
  isLoginMode.value = !isLoginMode.value;
  errorMessage.value = '';
};
</script>

<template>
  <div class="flex items-center justify-center min-h-[80vh]">
    <div class="w-full max-w-md p-8 bg-white rounded-lg shadow-md">
      <h1 class="text-2xl font-bold text-center mb-6 text-blue-600">
        {{ isLoginMode ? '管理员登录' : '用户注册' }}
      </h1>
      
      <div v-if="errorMessage" class="mb-4 p-3 bg-red-100 text-red-700 rounded-md">
        {{ errorMessage }}
      </div>
      
      <!-- 登录表单 -->
      <form v-if="isLoginMode" @submit.prevent="handleLogin" class="space-y-6">
        <div>
          <label for="mobile" class="block text-sm font-medium text-gray-700 mb-1">手机号</label>
          <input
            id="mobile"
            v-model="mobile"
            type="text"
            placeholder="请输入手机号"
            class="w-full px-4 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
            required
          />
        </div>
        
        <div>
          <label for="password" class="block text-sm font-medium text-gray-700 mb-1">密码</label>
          <input
            id="password"
            v-model="password"
            type="password"
            placeholder="请输入密码"
            class="w-full px-4 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
            required
          />
        </div>
        
        <button
          type="submit"
          class="w-full py-2 px-4 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 disabled:opacity-50"
          :disabled="isLoading"
        >
          {{ isLoading ? '登录中...' : '登录' }}
        </button>
        
        <div class="text-center mt-4">
          <button 
            type="button" 
            @click="toggleMode" 
            class="text-blue-600 hover:text-blue-800 text-sm font-medium"
          >
            没有账号？立即注册
          </button>
        </div>
      </form>
      
      <!-- 注册表单 -->
      <form v-else @submit.prevent="handleRegister" class="space-y-6">
        <!-- 头像上传 -->
        <div class="flex justify-center mb-4">
          <AvatarUpload v-model="avatarBase64" />
        </div>
        
        <div>
          <label for="mobile" class="block text-sm font-medium text-gray-700 mb-1">手机号</label>
          <input
            id="mobile"
            v-model="mobile"
            type="text"
            placeholder="请输入手机号"
            class="w-full px-4 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
            required
          />
        </div>
        
        <div>
          <label for="nickname" class="block text-sm font-medium text-gray-700 mb-1">昵称</label>
          <input
            id="nickname"
            v-model="nickname"
            type="text"
            placeholder="请输入昵称"
            class="w-full px-4 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
            required
          />
        </div>
        
        <div>
          <label for="password" class="block text-sm font-medium text-gray-700 mb-1">密码</label>
          <input
            id="password"
            v-model="password"
            type="password"
            placeholder="请输入密码"
            class="w-full px-4 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
            required
          />
        </div>
        
        <div>
          <label for="confirmPassword" class="block text-sm font-medium text-gray-700 mb-1">确认密码</label>
          <input
            id="confirmPassword"
            v-model="confirmPassword"
            type="password"
            placeholder="请再次输入密码"
            class="w-full px-4 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
            required
          />
        </div>
        
        <button
          type="submit"
          class="w-full py-2 px-4 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 disabled:opacity-50"
          :disabled="isLoading"
        >
          {{ isLoading ? '注册中...' : '注册' }}
        </button>
        
        <div class="text-center mt-4">
          <button 
            type="button" 
            @click="toggleMode" 
            class="text-blue-600 hover:text-blue-800 text-sm font-medium"
          >
            已有账号？立即登录
          </button>
        </div>
      </form>
    </div>
  </div>
</template>